﻿package Cain.game.clickFun.clickMc 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	import gs.TweenMax;
	import gs.easing.Linear;
	/**
	 * 飛行器
	 * @author Cain
	 */
	public class BirdMc extends Sprite 
	{
		private var mcName:String;
		private var st_x:Number;
		private var st_y:Number;
		private var ed_x:Number;
		private var ed_y:Number;
		private var s:Number;
		private var mc_class:Class;
		private var mc:DisplayObject;
		/**
		 * 飛行器
		 * @param	_mcName 顯示物件class
		 * @param	_st_x 起點x
		 * @param	_st_y 起點y
		 * @param	_ed_x 終點x
		 * @param	_ed_y 終點y
		 * @param	_s 時間
		 */
		public function BirdMc(_mcName:String,_st_x:Number,_st_y:Number,_ed_x:Number,_ed_y:Number,_s:Number) 
		{
			mcName = _mcName;
			st_x = _st_x;
			st_y = _st_y;
			ed_x = _ed_x;
			ed_y = _ed_y;
			s = _s;
			setup();
			addEventListener(Event.ADDED_TO_STAGE, addThis);
			
		}
		
		private function setup():void
		{
			mc_class = getDefinitionByName(mcName) as Class;
			mc = new mc_class();
			addChild(mc);
		}
		
		private function addThis(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addThis);
			addEventListener(Event.REMOVED_FROM_STAGE, removeThis);
			init();
		}
		
		private function init():void
		{
			x = st_x;
			y = st_y;
			TweenMax.to(this, s, { x:ed_x, y:ed_y ,ease:Linear.easeNone,onComplete:end} );
		}
		
		private function end():void
		{
			parent.removeChild(this);
		}
		
		private function removeThis(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, removeThis);
			
		}
	}
	
}